﻿using Microsoft.Practices.Prism.Commands;
using System;
using System.Windows.Input;
using System.Windows;

namespace HobSoft.SoccerBet2014.Client.ViewModels
{
    using dm = Domain.Models;
    using Domain.ClientService;
    public class BfAccount : CommandViewModel
    {
        public event EventHandler<BfAccount> OnDeleteRequest;

        #region command
        public ICommand SignupCommand { get; protected set; }
        public ICommand SignoutCommand { get; protected set; }
        public ICommand DeleteCommand { get; protected set; }
        public ICommand PickCertFileCommand { get; protected set; }
        #endregion

        public IBfRequestService bfRequestService;
        public BfAccount()
        {
            SignupCommand = new DelegateCommand(Signup, CanSignup);
            SignoutCommand = new DelegateCommand(Signout, CanSignout);
            DeleteCommand = new DelegateCommand(Delete, CanDelete);
            PickCertFileCommand = new DelegateCommand(PickCertificateFile);

            RegisterCommand(SignupCommand, "登录");
            RegisterCommand(SignoutCommand, "注销");
            RegisterCommand(DeleteCommand, "删除");
            RegisterCommand(PickCertFileCommand, "选择证书");
            OnValid = Valid();
        }

        #region command imple
        protected bool CanSignup()
        {
            return OnInitialized && !OnSignup && !CommandOnExecuting(SignupCommand);
        }
        public void Signup()
        {
            RaiseCommandStart(SignupCommand, "开始登录必发");
            RaiseCommandCanExecute(SignupCommand);
            OnSignup = true;
            if (bfRequestService != null)
            {
                var dmaccount = AutoMapper.Mapper.Map<dm.BFAccount>(this);
                bfRequestService.Singup(dmaccount, this.CertificateFile).ContinueWith(t =>
                {
                    Application.Current.Dispatcher.Invoke(new Action(delegate()
                    {
                        RaiseCommandOver(SignupCommand, "登录必发完成");
                        if (t.IsCanceled || t.Exception != null)
                        {
                            AccountState = "登录发生错误";
                            RaiseCommandCanExecute(SignupCommand);
                            OnSignup = false;
                            return;
                        }
                        if (!t.Result)
                        {
                            AccountState = "登录发生错误";
                            RaiseCommandCanExecute(SignupCommand);
                            OnSignup = false;
                            return;
                        }
                        OnSignup = true;
                        RaiseCommandCanExecute(SignupCommand);
                        RaiseCommandCanExecute(SignoutCommand);
                        RaiseCommandCanExecute(DeleteCommand);
                        AccountState = "登录成功";
                        PublicLog.InfoFormat("{0}登录成功", Account);
                    }));
                });
            }
        }

        protected bool CanSignout()
        {
            return OnInitialized && OnSignup;
        }
        public void Signout()
        {
            OnSignup = false;
            RaiseCommandCanExecute(SignoutCommand);
            RaiseCommandCanExecute(SignupCommand);
            RaiseCommandCanExecute(DeleteCommand);
        }

        protected bool CanDelete()
        {
            return !OnSignup;
        }
        public void Delete()
        {
            if (OnDeleteRequest != null)
                OnDeleteRequest(this, this);
        }

        public void PickCertificateFile()
        {
            Microsoft.Win32.OpenFileDialog openDialog = new Microsoft.Win32.OpenFileDialog();
            openDialog.DefaultExt = "*.crt";
            openDialog.Filter = "证书文档 (.crt)|*.crt";
            openDialog.Multiselect = false;
            openDialog.InitialDirectory = System.Environment.CurrentDirectory;
            openDialog.Title = "选择必发账号证书文件";
            var result = openDialog.ShowDialog(Application.Current.MainWindow);
            if (result.HasValue && result.Value)
            {
                CertificateFile = openDialog.FileName;
            }
        }
        #endregion

        public bool Valid()
        {
            return !string.IsNullOrEmpty(Account) && !string.IsNullOrEmpty(Password) && !string.IsNullOrEmpty(AppKey);
        }

        #region model property
        private bool _onValid;
        public bool OnValid
        {
            get { return _onValid; }
            set
            {
                PropertyValueChange(ref _onValid, value, "OnValid");
            }
        }

        private string _accountState;
        public string AccountState
        {
            get { return _accountState; }
            set
            {
                PropertyValueChange(ref _accountState, value, "AccountState");
            }
        }

        private bool _onSignup;
        public bool OnSignup
        {
            get { return _onSignup; }
            set
            {
                PropertyValueChange(ref _onSignup, value, "OnSignup");
            }
        }

        private string _account;
        public string Account
        {
            get { return _account; }
            set
            {
                PropertyValueChange(ref _account, value, "Account");
                OnValid = Valid();
            }
        }

        private string _password;
        public string Password
        {
            get { return _password; }
            set
            {
                PropertyValueChange(ref _password, value, "Password");
                OnValid = Valid();
            }
        }

        private string _session;
        public string Session
        {
            get { return _session; }
            set
            {
                PropertyValueChange(ref _session, value, "Session");
            }
        }

        private string _appKey;
        public string AppKey
        {
            get { return _appKey; }
            set
            {
                PropertyValueChange(ref _appKey, value, "AppKey");
                OnValid = Valid();
            }
        }

        private string _certificateFile;
        public string CertificateFile
        {
            get
            {
                return _certificateFile;
            }
            set
            {
                PropertyValueChange(ref _certificateFile, value, "CertificateFile");
            }
        }

        private dm.BFAccountUsage _usage;
        public dm.BFAccountUsage Usage
        {
            get { return _usage; }
            set
            {
                PropertyValueChange(ref _usage, value, "Usage");
            }
        }

        private decimal? _discount;
        public decimal? Discount
        {
            get { return _discount; }
            set { PropertyValueChange(ref _discount, value, "Discount"); }
        }
        #endregion
    }
}
